<!DOCTYPE html>
<html lang="en"
      xmlns="http://www.w3.org/1999/xhtml"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta content="webkit" name="renderer">
    <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
    <meta content="width=device-width, initial-scale=1, maximum-scale=1" name="viewport">
    <link href="/layui-v2.6.4/layui/css/layui.css" media="all" rel="stylesheet">
    <script charset="UTF-8" src="/js/jquery-3.5.1.min.js"></script>
</head>
<body>

<table class="layui-hide" id="test" lay-filter="test"></table>
<script id="toolbarDemo" type="text/html">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
        <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
        <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
    </div>
</script>
<script id="barDemo" type="text/html">
    <a class="layui-btn layui-btn-xs" lay-event="edit">更改权限</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">封禁</a>
</script>
<script charset="utf-8" src="/layui-v2.6.4/layui/layui.js"></script>

<script th:inline="javascript" type="text/javascript">
    var adminAuthor = /*[[${session.loginAdmin.getAdminLevel()}]]*/
        layui.use('table', function () {
            var table = layui.table;
            table.render({
                elem: '#test',
                url: 'getalladmin',
                parseData: function (res) { //res 即为原始返回的数据
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.msg, //解析提示文本
                        "count": res.count, //解析数据长度
                        "data": res.data //解析数据列表
                    }
                },
                page: true,
                limits: [3, 5, 10, 20],
                limit: 10,
                toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
                height: 'full-3',
                defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                    title: '提示',
                    layEvent: 'LAYTABLE_TIPS',
                    icon: 'layui-icon-tips'
                }],
                title: '管理员数据表',
                cols: [
                    [{
                        type: 'checkbox',
                        fixed: 'left'
                    }, {
                        field: 'adminId',
                        title: 'ID',
                        width: 50,
                        fixed: 'left',
                        unresize: true
                    }, {
                        field: 'adminNum',
                        title: '管理员账号',
                        width: 110,
                        edit: 'text'
                    }, {
                        field: 'adminPwd',
                        title: '管理员密码',
                        width: 200,
                        edit: 'text'
                    }, {
                        field: 'adminLevel',
                        title: '管理员权限',
                        width: 130,
                        edit: 'text',
                        sort: true
                    }, {
                        field: 'adminPhone',
                        title: '手机号',
                        width: 110,
                        edit: 'text',
                    }, {
                        field: 'adminHouse',
                        title: '家庭住址',
                        width: 100,
                        edit: 'text',
                    }, {
                        field: 'adminBirth',
                        title: '出生年月',
                        width: 150,
                        edit: 'text',
                    }, {
                        field: 'adminEmil',
                        title: '邮箱',
                        width: 150,
                        edit: 'text',
                    }, {
                        field: 'adminStatus',
                        title: '状态',
                        width: 80,
                        edit: 'text',
                        style: 'color:#269220'
                    }, {
                        fixed: 'right',
                        title: '操作',
                        toolbar: '#barDemo',
                        width: 150
                    }]
                ]
            });
            table.on('toolbar(test)', function (obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                switch (obj.event) {
                    case 'getCheckData':
                        var data = checkStatus.data;
                        layer.alert(JSON.stringify(data));
                        break;
                    case 'getCheckLength':
                        var data = checkStatus.data;
                        layer.msg('选中了：' + data.length + ' 个');
                        break;
                    case 'isAll':
                        layer.msg(checkStatus.isAll ? '全选' : '未全选');
                        break;
                    case 'LAYTABLE_TIPS':
                        layer.msg('这时管理员权限更改和查看页面');
                        break;
                }
            });

            //监听行工具事件
            table.on('tool(test)', function (obj) {
                var data = obj.data;
                if (obj.event === 'edit') {
                    if(adminAuthor!='root'){
                        layer.msg("权限不足!")
                    }else{
                        var level = data.adminLevel;
                        layer.prompt({
                            formType: 2,
                            value: data.adminLevel,
                            title: '最高管理员-->root\n\t超级管理员-->super\n\t普通管理员-->unfinished'
                        }, function (value, index) {
                            if (value != "root" && value != "unfinished" && value != "super") {
                                layer.msg("不存在您输入的权限!")
                            } else {
                                var adminid = data.adminId;
                                $.ajax({
                                    url: "/updateAdminAuthor"
                                    , type: 'get'
                                    , async: false
                                    , data: {
                                        id: adminid,
                                        author: value
                                    }, success: function (res) {
                                        if (res === "1") {
                                            obj.update({
                                                adminLevel: value
                                            });
                                            layer.msg("修改成功")
                                            layer.close(index);
                                        } else if (res === "0") {
                                            layer.msg("修改失败")
                                        }
                                    }
                                });
                                layer.close(index);
                            }
                        });
                    }
                } else {
                    //封禁
                    if (adminAuthor != 'root') {
                        if (data.adminLevel == 'root' || data.adminLevel == 'super') {
                            layer.msg("你没有权限封禁更高级的管理员!")
                        } else {
                            layer.confirm('确定么', function (index) {
                                var adminid = data.adminId;
                                var adminStatus = data.adminStatus;
                                $.ajax({
                                    url: "/updateAdminDel"
                                    , type: 'get'
                                    , async: false
                                    , data: {
                                        id: adminid,
                                        adminStatus: adminStatus
                                    }
                                    , success: function (res) {
                                        if (res === "1") {
                                            if (data.adminStatus == '正常') {
                                                obj.update({
                                                    adminStatus: "封锁"
                                                });
                                            } else {
                                                obj.update({
                                                    adminStatus: "正常"
                                                });
                                            }
                                            layer.msg("操作成功")
                                            layer.close(index);
                                        } else if (res === "0") {
                                            layer.msg("操作失败")
                                        }
                                    }
                                });
                            });
                        }
                    } else {
                        if (data.adminLevel == 'root') {
                            layer.msg("最高管理员不可被封禁!")
                        } else {
                            layer.confirm('确定么', function (index) {
                                var adminid = data.adminId;
                                var adminStatus = data.adminStatus;
                                $.ajax({
                                    url: "/updateAdminDel"
                                    , type: 'get'
                                    , async: false
                                    , data: {
                                        id: adminid,
                                        adminStatus: adminStatus
                                    }
                                    , success: function (res) {
                                        if (res === "1") {
                                            if (data.adminStatus == '正常') {
                                                obj.update({
                                                    adminStatus: "封锁"
                                                });
                                            } else {
                                                obj.update({
                                                    adminStatus: "正常"
                                                });
                                            }
                                            layer.msg("操作成功")
                                            layer.close(index);
                                        } else if (res === "0") {
                                            layer.msg("操作失败")
                                        }
                                    }
                                });
                            });
                        }
                    }
                }
            });
        });
</script>

</body>
</html>
